package com.ruoyi.project.dict.guhanyuapprove.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.TableSplitUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.guhanyu.domain.Guhanyu;
import com.ruoyi.project.dict.guhanyu.mapper.GuhanyuMapper;
import com.ruoyi.project.dict.guhanyuapprove.domain.GuhanyuApprove;
import com.ruoyi.project.dict.guhanyuapprove.mapper.GuhanyuApproveMapper;
import com.ruoyi.project.dict.guhanyuapprove.service.IGuhanyuApproveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 古汉语审核Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-05-19
 */
@Service
@DataSource(DataSourceType.DICT)
public class GuhanyuApproveServiceImpl implements IGuhanyuApproveService {

    @Autowired
    private GuhanyuApproveMapper guhanyuApproveMapper;

    @Autowired
    private GuhanyuMapper guhanyuMapper;


    /**
     * 查询古汉语审核
     *
     * @param id 古汉语审核ID
     * @return 古汉语审核
     */
    @Override
    public GuhanyuApprove selectGuhanyuApproveById(Long id) {
        return guhanyuApproveMapper.selectGuhanyuApproveById(id);
    }

    /**
     * 查询古汉语审核列表
     *
     * @param guhanyuApprove 古汉语审核
     * @return 古汉语审核
     */
    @Override
    public List<GuhanyuApprove> selectGuhanyuApproveList(GuhanyuApprove guhanyuApprove) {
        return guhanyuApproveMapper.selectGuhanyuApproveList(guhanyuApprove);
    }

    /**
     * 新增古汉语审核
     *
     * @param guhanyuApprove 古汉语审核
     * @return 结果
     */
    @Override
    public int insertGuhanyuApprove(GuhanyuApprove guhanyuApprove) {
        return guhanyuApproveMapper.insertGuhanyuApprove(guhanyuApprove);
    }

    /**
     * 修改古汉语审核
     *
     * @param guhanyuApprove 古汉语审核
     * @return 结果
     */
    @Override
    public int updateGuhanyuApprove(GuhanyuApprove guhanyuApprove) {
        int ret = guhanyuApproveMapper.updateGuhanyuApprove(guhanyuApprove);
        if (guhanyuApprove.getAutoApprove() == 1) {
            this.approve(guhanyuApprove.getId(), 1);
        }
        return ret;
    }

    /**
     * 删除古汉语审核对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGuhanyuApproveByIds(String ids) {
        return guhanyuApproveMapper.deleteGuhanyuApproveByIds(Convert.toStrArray(ids));
    }

    @Override
    public int approve(Long id, Integer status) {
        GuhanyuApprove guhanyuApprove = guhanyuApproveMapper.selectGuhanyuApproveById(id);
        guhanyuApprove.setApproveSt(status);
        guhanyuApprove.setApproveTime(DateUtils.getNowDate());
        guhanyuApprove.setApproveUser(ShiroUtils.getLoginName());

        if (status == 1) {
            Guhanyu guhanyu = new Guhanyu();
            guhanyu.setText(guhanyuApprove.getText());
            guhanyu.setContent(guhanyuApprove.getContent());
            guhanyu.setKkid(guhanyuApprove.getKkid());
            guhanyu.setCreatedTime(guhanyuApprove.getCreatedTime());
            guhanyu.setTable(TableSplitUtil.getGuhanyuTable(guhanyuApprove.getText()));
            guhanyuMapper.insertGuhanyu(guhanyu);
        }
        return guhanyuApproveMapper.updateGuhanyuApprove(guhanyuApprove);
    }

    /**
     * 删除古汉语审核信息
     *
     * @param id 古汉语审核ID
     * @return 结果
     */
    @Override
    public int deleteGuhanyuApproveById(Long id) {
        return guhanyuApproveMapper.deleteGuhanyuApproveById(id);
    }
}
